/**
 * related to selectArea.ui
 * 
 * @Author : chenglm
 * @Timestamp : 2016-07-22
 */
var app = sm("do_App");
var page = sm("do_Page");
var global = deviceone.sm("do_Global");
var hashdata = mm("do_HashData");
var categoryList = ui("categoryList");
var listdata = mm("do_ListData");
categoryList.bindItems(listdata);

var common = require("common");
var mallUrl = require("url/mall");
var qqHttp = require("helper/http");
var httpSilence = require("helper/httpSilence");
var param = {};
var shopCategoryCount = 1;
var nowCategory = "";
//andriod返回键
page.on("back", function() {
    app.closePage();
});

/* 头部 */
var headerAout = ui("do_ALayout_header");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
    app.closePage()
});
var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "主营类目"
})
header.refreshData();

//页面装载完成后，开始初始化工作
page.on("loaded", function(){
	shopCategoryCount = parseInt(global.getMemory("shopCategoryCount"));
	global.setMemory("shopCategoryCount", shopCategoryCount+1);
	//读取当前页面的传入参数
	var jsonData = page.getData();
	param.parentId = jsonData.parentId;
	param.categoryStr = jsonData.categoryStr;
	param.type = jsonData.type;
	qqHttp.post(mallUrl.url.shopCategory.childrenList, {parentId:jsonData.parentId,type:jsonData.type}, function(data) {
		if (data.childrenList != null && data.childrenList.length > 0) {
			listdata.addData(data.childrenList);
		}
		categoryList.refreshItems();
	});
});

//点击一条
categoryList.on("touch", function(data){
	var one = listdata.getOne(data);
	param.parentId = one.categoryId;
	nowCategory = one.name;
	if (param.categoryStr == '') {
		param.categoryStr = one.name;
	} else {
		param.categoryStr = param.categoryStr+'-'+one.name;
	}
	httpSilence.post(mallUrl.url.shopCategory.isLast, {"categoryId":one.categoryId}, function(data2) {
		if (data2.isLast == 1) { //是最后一层
			param.fromPage = 'selectShopCategory';
			app.closePage(param,null,global.getMemory("shopCategoryCount"));
		} else {
			common.open("source://view/qshop/manage/shop/selectCategory.ui",param);
		}
	});
});
page.on("result", function(data) {
	param.categoryStr = param.categoryStr.replace(nowCategory,'');
	shopCategoryCount = parseInt(global.getMemory("shopCategoryCount"));
	global.setMemory("shopCategoryCount", shopCategoryCount-1);
});